const app = getApp()
const { processImageUrl } = require('../../../utils/image')
const request = require('../../../utils/request.js');
Page({
  data: {
    orderId: '',
    order: null,
    loading: true
  },

  onLoad(options) {
    if (options.id) {
      this.setData({
        orderId: options.id
      })
      this.loadOrderDetail()
    }
  },

  // 加载订单详情
  loadOrderDetail() {
    wx.showLoading({
      title: '加载中...'
    })

    request.request({
      url: `${app.globalData.baseUrl}/api/orders/${this.data.orderId}`,
      method: 'GET',
      header: {
        'Authorization': `Bearer ${app.getGlobalToken()}`
      },
      success: (res) => {
        const data = res.data.data
        data.residue.image = processImageUrl(data.residue.image)
        this.setData({
          order: data
        })
      },
      fail: (err) => {
        wx.showToast({
          title: '加载失败',
          icon: 'none'
        })
        console.error('加载订单详情失败:', err)
      },
      complete: () => {
        wx.hideLoading()
      }
    })
  },

  // 取消订单
  cancelOrder() {
    wx.showModal({
      title: '提示',
      content: '确定要取消该订单吗？',
      success: (res) => {
        if (res.confirm) {
          wx.showLoading({ title: '处理中' })
          // 调用取消订单接口
          request.request({
            url: `${app.globalData.baseUrl}/api/orders/${this.data.orderId}/cancel`,
            method: 'POST',
            header: {
              'Authorization': `Bearer ${app.getGlobalToken()}`
            },
            success: () => {
              wx.showToast({
                title: '订单已取消',
                icon: 'success'
              })
              this.loadOrderDetail()
            },
            fail: () => {
              wx.showToast({
                title: '订单取消失败',
                icon: 'none'
              })
            },
            complete:() => {
              wx.hideLoading()
            }
          })
        }
      }
    })
  },

  // 支付订单
  payOrder() {
    const id = this.data.orderId
    wx.navigateTo({
      url: `/pages/orders/pay/pay?id=${id}`
    })
  },

  // 删除订单
  deleteOrder() {
    wx.showModal({
      title: '提示',
      content: '确定要删除该订单吗？',
      success: (res) => {
        if (res.confirm) {
          wx.showLoading({ title: '处理中' })
          request.request({
            url: `${app.globalData.baseUrl}/api/orders/${this.data.orderId}/delete`,
            method: 'POST',
            header: {
              'Authorization': `Bearer ${app.getGlobalToken()}`
            },
            success: () => {
              wx.showToast({
                title: '订单已删除',
                icon: 'success'
              })
              app.globalData.needRefresh = true;
              setTimeout(() => {
                wx.navigateBack()
              }, 1500)
            },
            fail: () => {
              wx.showToast({
                title: '订单删除失败',
                icon: 'none'
              })
            },
            complete:() => {
              wx.hideLoading()
            }
          })
        }
      }
    })
  }
}) 